/*本模块功能:成绩管理
        编写者:朱光曜
        编写时间:2017.11.03
        修改：杨锡君
        20180426
        */
$(function() {
    var string1 = '<tr>' +
        '<th class="col-lg-2">序号</th>' +
        '<th class="col-lg-5">班级/申请班</th>' +
        '<th class="col-lg-6">课程</th>' +
        '<th class="col-lg-3">学生姓名</th>' +
        '<th class="col-lg-3">平时成绩</th>' +
        '<th class="col-lg-3">总成绩</th>' +
        '<th class="col-lg-3">状态</th>' +
        '<th class="col-lg-3">操作</th>' +
        '</tr>';
    //列表头部
    var loading1 = new Loading();//监控全局ajax添加loading
    loading1.init();
    $(document).ajaxSend(function(){
        loading1.show();

    });
    $(document).ajaxStop(function(){
        loading1.hide();

    });
    var confirm1 = new Confirm;
    var Alert1 = new Alert;
    var Alert2 = new Alert;
    function tabelModel(data) {//列表内容
        var preforSource = "";
//	    if( data.memberList[key].preforSource==0){
//	    	preforSource="暂无统计";
//	    }else{
        preforSource=data.memberList[key].preforSource;
        //}
        var sumSource="";
        /*if( data.memberList[key].sumSource==0){
            sumSource="暂无统计";
        }else{*/
        sumSource=data.memberList[key].sumSource;
        //}
        var detailsClick="";
        if(data.memberList[key].classEndMark==0){
            detailsClick="details";
        }else{
            detailsClick="";
        }
        return '<tr data-num="0" classId="' + data.memberList[key].classId + '" courseId="' + data.memberList[key].courseId + '"  stuorteaId="' + data.memberList[key].stuorteaId + '">' +
            '<td class="col-lg-2">' + ((parseInt(key) + 1)+(data.pageNow-1)*10) + '</td>' +
            '<td class="col-lg-2 sexName"  title="' + data.memberList[key].className + '">' + data.memberList[key].className + '</td>' +
            '<td class="col-lg-6"  title="' + data.memberList[key].courseName + '">' + data.memberList[key].courseName + '</td>' +
            '<td class="col-lg-5" title="' + data.memberList[key].userName + '">' + data.memberList[key].userName + '</td>' +
            '<td class="col-lg-3"  title="' + preforSource + '">' + preforSource + '分</td>' +
            '<td class="col-lg-3"  title="' + sumSource + '">' + sumSource + '分</td>' +
            '<td class="col-lg-3"  title="' + data.memberList[key].classEndName + '">' + data.memberList[key].classEndName + '</td>' +
            '<td class="col-lg-4">' +
            '<a href="javascript:;" class="'+detailsClick+'">详情</a>' +
            '</td>' +
            '</tr>';

    }






    var nowData = 1;
    var index = {
        init: function() {
            this.bind();
        },
        bind: function() {
            var self = this;
            this.data_content = {}; //搜索集
            this.tab_content = {}; //列表集
            this.parameter = {
                "pageNow": 1,
                "pageSize": 10,
                "classEndMark":3
            };

            this.postData(this.parameter, nowData);
            $(document).off("click",".alert_img").on("click",".alert_img",function(){//点击删除id
                $(".alert").removeAttr("id");
            });

            $(document).off("click",".cancel").on("click",".cancel",function(){//点击删除id
                $("#Popup").hide();
                // $(".pop_main h5").remove();
                // $("#teacherMessage").val("");
            });


            $(document).on("change","#department", function() { //院系联动专业

                if($("#department option:selected").attr("departmentId") == null) {

                    $("#major").html('<option value="1" >请选择</option>');
                } else {

                    var parameter = {
                        "departmentId": $("#department option:selected").attr("departmentId")
                    };

                    $.ajax({
                        type: "post",
                        url: "getProfessionListByDepmentId",
                        async: true,
                        data: {
                            "jsonStr": JSON.stringify(parameter)
                        },
                        success: function(data) {
                            //获取地点

                            var msgNew = JSON.parse(data);
                            var con = "";

                            for(key in msgNew.professionList) {
                                con += '<option value="1" professionId="' + msgNew.professionList[key].professionId + '">' + msgNew.professionList[key].professionName + '</option>';
                            }
                            $("#major").html(con);

                        },
                        error: function(data, status, xhr) {
                            var json = JSON.stringify({
                                "pageName": "error"
                            });
                            window.location.href = "skipPage?jsonStr=" + json;

                        }
                    });
                }
            });
            $(".initAchievement").off("click").click(function(){//点击展示
                $("#Popup").show();
                $("#teacherMessage").val("");
                $("#Popup .pop_main h5").remove();
            });
            $("#Popup .popup_contenthead span").off("click").click(function(){//点击隐藏

                $("#Popup").hide();
            });

            function getFileName(o) {//获得文件名
                var pos = o.lastIndexOf("\\");
                return o.substring(pos + 1);
            }
            $(document).on("change","#Popup #teacherMessage",function(){//上传文件
                //上传文件验证
                if($(this).val() != ""){
                    var type = $(this).val().substring($(this).val().lastIndexOf(".")).toLowerCase(); // 取文件格式后缀名
                    if(type != ".xls" && type != ".xlsx") {
                        $("#teacherMessage").val("");
                        self.alert1("请上传Excel文件，谢谢合作！","fail");

                        return false;
                    } else {


                        var filename = $(this).val().substring($(this).val().lastIndexOf("\\") + 1);
                        $("#Popup .pop_main h5").remove();
                        $("#Popup .pop_main").append("<h5>已选&ensp;:" + filename+ "</h5>");

                        return true;
                    }
                }else{
                    $("#teacherMessage").val("");
                    $("#Popup .pop_main h5").remove();
                }
            });
            $("#addOneChapter").submit(function (event) {

                if ($("#Popup h5").html() ==null) {
                    self.alert1('请选择文件');
                    return false;
                }
                var formData = new FormData(this);

                $.ajax({
                    url: "imStuSoruce",
                    type: 'post',
                    data: formData,
                    processData: false,
                    contentType: false,
                    beforeSend: function() {
                        //console.log("正在进行，请稍候");
                    },
                    success: function(responseStr) {
                        //console.log(responseStr);
                        var responseStr=JSON.parse(responseStr);
                        if(responseStr.flag ===true) {
                            self.alerty("上传成功");
                            // window.location.reload(true);

                        } else {

                            //self.alert1(responseStr.msg);
                            if(responseStr.msg){
                                self.alert1(responseStr.msg);
                            }else{
                                self.alert1("上传失败");
                            }
                            $("#teacherMessage").val("");
                            // $("#file").val("");
                            $("#Popup .pop_main h5").remove();
                            $("#Popup").show();
                        }
                        return false;

                    },
                    error: function(responseStr) {
                        var json = JSON.stringify({
                            "pageName": "error"
                        });
                        window.location.href = "skipPage?jsonStr=" + json;
                    }
                });
                $("#Popup").hide();
                return false;
            });
        },
        postData: function(par_data, nowData,hunt) { //获得搜索及列表数据
            var self = this;
            var parameter = par_data;

            $.ajax({
                type: "post",
                url: "getAllSourceList",
                async: true,
                data: {
                    "jsonStr": JSON.stringify(parameter)
                },
                success: function(data, status, xhr) {
                    var new_data = JSON.parse(data);
                    if(new_data.result == "success"){
                        if(hunt) {
                            self.tab_content = new_data;

                            self.tab_add();
                        } else {

                            self.data_content = new_data;
                            self.tab_content = new_data;

                            self.add();
                            self.tab_add();
                        }
                        var con = "";
                        var c = '<option value="0" courseid="">请选择</option>';
                        for(key in new_data.courseList) {
                            con += '<option value="' + new_data.courseList[key].courseId + '" courseId="' + new_data.courseList[key].courseId + '">' + new_data.courseList[key].courseName + '</option>';
                        }
                        $("#curriculumtNames").html(c+con);
                        if(new_data.searchObejct.classEndMark!=null && new_data.searchObejct.classEndMark!=undefined && new_data.searchObejct.classEndMark!=''){
                            $("#state").val(new_data.searchObejct.classEndMark);
                        }
                        if(new_data.searchObejct.classId!=null && new_data.searchObejct.classId!=undefined && new_data.searchObejct.classId!=''){
                            $("#className").val(new_data.searchObejct.classId);
                        }else{
                            $("#className").val(0);
                        }
                        if(new_data.searchObejct.courseId!=null && new_data.searchObejct.courseId!=undefined && new_data.searchObejct.courseId!=''){
                            $("#curriculumtNames").val(new_data.searchObejct.courseId);
                        }else{
                            $("#curriculumtNames").val(0);
                        }
                        var sumpage = new_data.totalPage;
                        if(sumpage<1){sumpage=1;};
                        var nowpage = new_data.pageNow;
                        var pagenumber = 10;
                        var parame = parameter;
                        $(".tcdPageCode").createPage({
                            pageCount: sumpage, //总页数
                            current: nowpage, //当前页数
                            pageNumber: pagenumber, //每页显示数量
                            backFn: function(p) {

                                nowpage = p;

                                if(nowpage >= sumpage) {
                                    nowpage = sumpage;
                                }
                                if(!parameter) {
                                    parameter = {
                                        "pageNow": nowpage,
                                        "pageSize": 10
                                    };

                                } else {
                                    parameter.pageNow = nowpage;

                                }
                                $.ajax({
                                    type: "post",
                                    url: "getAllSourceList",
                                    async: true,
                                    data: {
                                        "jsonStr": JSON.stringify(parameter)
                                    },
                                    async: true,
                                    success: function(msg) {
                                        //获取院系
                                        var new_data = JSON.parse(msg);
                                        self.tab_content = new_data;

                                        self.tab_add();
                                        var con = "";
                                        var c = '<option value="0" courseid="">请选择</option>';
                                        for(key in new_data.courseList) {
                                            con += '<option value="' + new_data.courseList[key].courseId + '" courseId="' + new_data.courseList[key].courseId + '">' + new_data.courseList[key].courseName + '</option>';
                                        }
                                        $("#curriculumtNames").html(c+con);
                                        if(new_data.searchObejct.classEndMark!=null && new_data.searchObejct.classEndMark!=undefined && new_data.searchObejct.classEndMark!=''){
                                            $("#state").val(new_data.searchObejct.classEndMark);
                                        }
                                        if(new_data.searchObejct.classId!=null && new_data.searchObejct.classId!=undefined && new_data.searchObejct.classId!=''){
                                            $("#className").val(new_data.searchObejct.classId);
                                        }else{
                                            $("#className").val(0);
                                        }
                                        if(new_data.searchObejct.courseId!=null && new_data.searchObejct.courseId!=undefined && new_data.searchObejct.courseId!=''){
                                            $("#curriculumtNames").val(new_data.searchObejct.courseId);
                                        }else{
                                            $("#curriculumtNames").val(0);
                                        }
                                    },
                                    error: function(data, status, xhr) {
                                        var json = JSON.stringify({
                                            "pageName": "error"
                                        });
                                        window.location.href = "skipPage?jsonStr=" + json;
                                    }
                                });
                            }
                        });
                    }else{
                        if(new_data.failMsg == "暂无权限"){
                            parent.location.reload(true);
                        }
                    }

                },
                error: function(data, status, xhr) {
                    var json = JSON.stringify({
                        "pageName": "error"
                    });
                    window.location.href = "skipPage?jsonStr=" + json;

                }
            });
        },
        add: function() { //搜索追加
            var self = this;
            var data = self.data_content;
            for(key in data.classList) {

                $("#className").append("<option value='"+  data.classList[key].classId +"' classId=" + data.classList[key].classId + ">" + data.classList[key].className + "</option>");
            }
            // $("#className").on("change", function() { //班级联动课程
            //     if($("#className option:selected").attr("classId") == "") {
            //
            //         $("#curriculumtNames").html('<option value="1" courseId="">请选择</option>');
            //     } else {
            //
            //         var parameter = {
            //             "classId": $("#className option:selected").attr("classId")
            //         };
            //
            //         $.ajax({
            //             type: "post",
            //             url: "getAllCourseListByClassId",
            //             async: true,
            //             data: {
            //                 "jsonStr": JSON.stringify(parameter)
            //             },
            //             success: function(data) {
            //                 //获取地点
            //
            //                 var msgNew = JSON.parse(data);
            //                 var con = "";
            //
            //                 for(key in msgNew.courseList) {
            //                     con += '<option value="1" courseId="' + msgNew.courseList[key].courseId + '">' + msgNew.courseList[key].courseName + '</option>';
            //                 }
            //                 $("#curriculumtNames").html(con);
            //
            //             },
            //             error: function(data, status, xhr) {
            //                 var json = JSON.stringify({
            //                     "pageName": "error"
            //                 });
            //                 window.location.href = "skipPage?jsonStr=" + json;
            //
            //             }
            //         });
            //     }
            // });
            $(".funBox .searchBtn").off("click").click(function() { //搜索点击

                var classId = $("#className option:selected").attr("classId");
                var courseId = $("#curriculumtNames option:selected").attr("courseId");
                var classEndMark=  $("#state option:selected").val();

                var parameter = {
                    "classId": classId,
                    "courseId": courseId,
                    "classEndMark":classEndMark,
                    "pageNow": 1,
                    "pageSize": 10
                };
                //遍历删除空元素
                for(i in parameter){

                    if(parameter[i]==null||parameter[i]==""){
                        delete parameter[i];
                    }
                    if(!isNaN(parameter[i])){
                        parameter[i]=Number(parameter[i]);
                    }
                }



                self.postData(parameter, nowData,1);
            });
            $(".loadAchievement").off("click").click(function(){//导出成绩
                if($(".tableBox ").html().indexOf("暂无数据") == -1){
                    var classId = $("#className option:selected").attr("classId");

                    var courseId =$("#curriculumtNames option:selected").attr("courseId");
                    var classEndMark=$("#state option:selected").val();
                    var parameter = {
                        "classId": classId,
                        "courseId": courseId,
                        "classEndMark":Number(classEndMark),
                        "pageNow": 0,
                        "pageSize": 10
                    };
                    for(i in parameter){
                        if(parameter[i]!==0){
                            if(parameter[i]==null||parameter[i]==""){
                                delete parameter[i];
                            }
                        }

                    }

                    window.location.href="sourceExcel?jsonStr="+JSON.stringify(parameter);
                }else{
                    Alert2.content = "当前暂无数据";
                    Alert2.init();
                    Alert2.show();
                }

            });
        },
        tab_add: function() { //列表追加

            var self = this;
            var data = self.tab_content;
            var table = string1;


            var i = 0;
            for(key in data.memberList) {
                i++;
            }

            if(i > 0) {

                for(key in data.memberList) {

                    table += tabelModel(data);
                }

                $(".mainContent .tableBox").html(table);
            } else {
                var trImg = "<tr><td colspan='8'><img style='margin: 80px auto 20px;' src='pc/images/ico_noneData.png'><p style='font-size:26px;color:#8a8a8a;width:100%;text-align:center;'>暂无数据</p></td/tr>";
                $(".mainContent .tableBox").html(string1 + trImg);
            }

            //设置列表宽度(最大为9)
            var numMax = 0;
            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));
                numMax += num1;
            });

            $(".tableBox tr th").each(function() {
                var str = $(this).attr("class");
                var num = str.lastIndexOf("col-lg-") + 7;
                var num1 = parseFloat(str.substring(num, num + 1));

                $(this).css("width", num1 / numMax * 100 + "%");
            });


            $(".details").off("click").click(function() { //跳页
                var json = JSON.stringify({
                    "pageName": "teacherGradeManagementMessage",
                    "paramData": {
                        "classId": $(this).parents("tr").attr("classId"),
                        "courseId": $(this).parents("tr").attr("courseId"),
                        "stuorteaId": $(this).parents("tr").attr("stuorteaId")
                    }
                });
                window.location.href = "skipPage?jsonStr=" + json;

            });
        },
        alert1: function(content1) { //confirm提示

            Alert1.contenthead = "提示";
            Alert1.content = content1;

            Alert1.success = function() {

                Alert1.hide();
            };


            Alert1.init();
            Alert1.show();
        },
        alerty: function(content1) { //confirm提示

            Alert1.contenthead = "提示";
            Alert1.content = content1;

            Alert1.success = function() {

                window.location.reload(true);
            };


            Alert1.init();
            Alert1.show();
        }



    };
    index.init();

});